<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" :model="form" :rules="rules" label-width="220px">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="form.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号">
                <el-input v-model="form.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input v-model="form.mobile" :disabled="!!$route.params.id" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <select-tree v-model="form.departmentId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="form.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="form.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="form.correctionTime"
                  size="mini"
                  value-format="yyyy-MM-dd"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <image-upload v-model="form.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="updateEmployee">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import selectTree from './components/select-tree.vue'
import { addEmployee, getEmployeeDefailt, editEmployeeDefailt } from '@/api/employee'
import ImageUpload from './components/image-upload.vue'
export default {
  components: { selectTree, ImageUpload },
  data() {
    return {
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentId: null,
        timeOfEntry: '',
        correctionTime: '',
        staffPhoto: null
      },
      rules: {
        username: [
          { required: true, message: '员工名字不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '必须1-4位字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        // workNumber: [
        //   { required: true, message: '工号不能为空', trigger: 'blur' }
        // ],
        departmentId: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '转正时间不能为空', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (this.form.timeOfEntry) {
                if (new Date(this.form.timeOfEntry) > new Date(value)) {
                  callback(new Error('转正时间不能小于入职时间'))
                } else {
                  callback()
                }
              }
            }
          }
        ]
      }
    }
  },
  created() {
    this.$route.params.id && this.getEmployeeDefailt()
  },
  methods: {
    updateEmployee() {
      this.$refs.userForm.validate(async valid => {
        if (valid) await this.addEmployee()
      })
    },
    async addEmployee() {
      if (this.$route.params.id) {
        const res = await editEmployeeDefailt(this.form)
        this.$message.success(res.message)
        this.$router.push('/employee')
      }
      const res = await addEmployee(this.form)
      this.$message.success(res.message)
      this.$router.push('/employee')
    },
    async getEmployeeDefailt() {
      const res = await getEmployeeDefailt(this.$route.params.id)
      this.form = res.data
    }
  }
}
</script>

<style scoped lang="scss">
.edit-form {
  background: #fff;
  padding: 20px;
  .inputW {
    width: 380px;
  }
}
</style>
